package cn.iocoder.an.module.business.controller.admin.storeinit;

import cn.iocoder.an.module.business.controller.admin.drawconfig.vo.DrawConfigImportVo;
import cn.iocoder.an.module.business.controller.admin.equipmentmanage.vo.ImportRespVO;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.storeinit.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.storeinit.StoreInitDO;
import cn.iocoder.an.module.business.service.storeinit.StoreInitService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 期初库存台账")
@RestController
@RequestMapping("/business/store-init")
@Validated
public class StoreInitController {

    @Resource
    private StoreInitService storeInitService;

    @PostMapping("/create")
    @Operation(summary = "创建期初库存台账")
    @PreAuthorize("@ss.hasPermission('business:store-init:create')")
    public CommonResult<Long> createStoreInit(@Valid @RequestBody StoreInitSaveReqVO createReqVO) {
        return success(storeInitService.createStoreInit(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新期初库存台账")
    @PreAuthorize("@ss.hasPermission('business:store-init:update')")
    public CommonResult<Boolean> updateStoreInit(@Valid @RequestBody StoreInitSaveReqVO updateReqVO) {
        storeInitService.updateStoreInit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除期初库存台账")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:store-init:delete')")
    public CommonResult<Boolean> deleteStoreInit(@RequestParam("id") Long id) {
        storeInitService.deleteStoreInit(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除期初库存台账")
                @PreAuthorize("@ss.hasPermission('business:store-init:delete')")
    public CommonResult<Boolean> deleteStoreInitList(@RequestParam("ids") List<Long> ids) {
        storeInitService.deleteStoreInitListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得期初库存台账")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:store-init:query')")
    public CommonResult<StoreInitRespVO> getStoreInit(@RequestParam("id") Long id) {
        StoreInitDO storeInit = storeInitService.getStoreInit(id);
        return success(BeanUtils.toBean(storeInit, StoreInitRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得期初库存台账分页")
    @PreAuthorize("@ss.hasPermission('business:store-init:query')")
    public CommonResult<PageResult<StoreInitRespVO>> getStoreInitPage(@Valid StoreInitPageReqVO pageReqVO) {
        PageResult<StoreInitDO> pageResult = storeInitService.getStoreInitPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StoreInitRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出期初库存台账 Excel")
    @PreAuthorize("@ss.hasPermission('business:store-init:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStoreInitExcel(@Valid StoreInitPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreInitDO> list = storeInitService.getStoreInitPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "期初库存台账.xlsx", "数据", StoreInitRespVO.class,
                        BeanUtils.toBean(list, StoreInitRespVO.class));
    }




    @GetMapping("/get-import-template")
    @Operation(summary = "导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<DrawStoreInitImportVo> list = Arrays.asList(
                DrawStoreInitImportVo.builder().customerAccount("A001").customerName("江铃股份").storeLocation("南昌")
                        .storeName("南昌中联物流").drawingNumber("RS1-9461-BA").productName("废气进气管").initAmount(100).build(),
                DrawStoreInitImportVo.builder().customerAccount("A001").customerName("江铃股份").storeLocation("南昌")
                        .storeName("南昌中联物流").drawingNumber("RS1-9461-CC").productName("废气进气管").initAmount(100).build(),
                DrawStoreInitImportVo.builder().customerAccount("A001").customerName("江铃股份").storeLocation("南昌")
                        .storeName("南昌中联物流").drawingNumber("RS1-9461-DD").productName("废气进气管").initAmount(100).build()
        );
        // 输出
        ExcelUtils.write(response, "导入模板.xlsx", "列表", DrawStoreInitImportVo.class, list);
    }


    @PostMapping("/import")
    @Operation(summary = "导入")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    public CommonResult<ImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                  @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<DrawStoreInitImportVo> list = ExcelUtils.read(file, DrawStoreInitImportVo.class);
        return success(storeInitService.importList(list, updateSupport));
    }


}